package mark.tom.pistols.algorithm.signature;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;

import mark.tom.pistols.algorithm.encryption.Base64Encryption;

public final class RSASignature {

    public static byte[] sign(String content, String privateKey) throws Exception {
        return sign(content, privateKey, "utf8");
    }

    public static byte[] sign(String content, String privateKey, String encode) throws Exception {
        PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64Encryption.decrypt(privateKey));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey priKey = keyFactory.generatePrivate(priPKCS8);
        Signature signature = Signature.getInstance("SHA1WithRSA");
        signature.initSign(priKey);
        signature.update(content.getBytes(encode));
        return signature.sign();
    }
}
